package com.clss.emergencycall.base;

import android.location.Location;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdate;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.MapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.CameraPosition;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.maps.model.MyLocationStyle;
import com.amap.api.services.help.Inputtips;
import com.amap.api.services.help.InputtipsQuery;
import com.clss.emergencycall.R;
import com.clss.emergencycall.bean.EmergencyLocation;
import com.clss.emergencycall.utils.ImageUtil;
import com.clss.emergencycall.utils.Lg;
import com.clss.emergencycall.utils.SpHelper;

public abstract class BaseMapActivity extends BaseActivity implements AMap.OnMyLocationChangeListener
        , AMap.OnMapClickListener, AMap.OnMarkerClickListener, AMap.InfoWindowAdapter {
    private static final String TAG = "BaseMapActivity";
    public AMap aMap = null;
    private MyLocationStyle mMyLocationStyle;
    public EmergencyLocation localLocation = SpHelper.getInstance().getLocation();

    /**
     * 初始化地图
     */
    public void initMap(Bundle savedInstanceState, MapView mapView) {
        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();
        mMyLocationStyle = new MyLocationStyle();
        mMyLocationStyle.interval(5000);
        mMyLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE_NO_CENTER);
        mMyLocationStyle.myLocationIcon(BitmapDescriptorFactory.fromResource(R.drawable.gps_point));
        aMap.setMyLocationStyle(mMyLocationStyle);
        aMap.setInfoWindowAdapter(this);
        aMap.setMyLocationEnabled(true);
        aMap.setOnMyLocationChangeListener(this);
        aMap.setOnMapClickListener(this);
        aMap.setOnMarkerClickListener(this);
        aMap.animateCamera(CameraUpdateFactory.zoomTo(aMap.getMaxZoomLevel() - 3));
    }

    public void initNoLocMap(Bundle savedInstanceState, MapView mapView) {
        mapView.onCreate(savedInstanceState);
        aMap = mapView.getMap();
        aMap.setMyLocationEnabled(true);
        aMap.setOnMapClickListener(this);
        aMap.animateCamera(CameraUpdateFactory.zoomTo(aMap.getMaxZoomLevel() - 3));
    }

    /**
     * 移动相机到某个位置
     */
    public void moveCamera(String lat, String lng) {
        CameraUpdate mCameraUpdate = CameraUpdateFactory.newCameraPosition(
                new CameraPosition(new LatLng(Double.valueOf(lat), Double.valueOf(lng)), 16f, 0f, 0f));
        aMap.moveCamera(mCameraUpdate);
    }

    /**
     * 添加marker点
     */
    public Marker addMarker(String mLat, String mLng, View view, String peopleId) {
        MarkerOptions options = new MarkerOptions().anchor(0.5f, 1)
                .position(new LatLng(Double.valueOf(mLat), Double.valueOf(mLng)))
                .icon(BitmapDescriptorFactory.fromBitmap(ImageUtil.convertViewToBitmap(view))).draggable(true).setFlat(true);
        Marker marker = aMap.addMarker(options);
        if (!TextUtils.isEmpty(peopleId))
            marker.setObject(peopleId);
        return marker;
    }

    /***
     * 获取一个橙色view显示在marker上面
     */
    public View getOrangeView(String name) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_marker_base, null);
        RelativeLayout rl = view.findViewById(R.id.rl_main);
        rl.setBackgroundResource(R.mipmap.team_btn_location_orange_one);
        TextView mTvName = view.findViewById(R.id.tv_name);
        mTvName.setText(name);
        mTvName.setTextColor(ContextCompat.getColor(this, R.color.orange_f47b41));
        return view;
    }

    /***
     * 获取一个橙色view显示在marker上面
     */
    public View getRedView(String name) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_marker_base, null);
        RelativeLayout rl = view.findViewById(R.id.rl_main);
        rl.setBackgroundResource(R.mipmap.team_btn_location_red_one);
        TextView mTvName = view.findViewById(R.id.tv_name);
        mTvName.setText(name);
        mTvName.setTextColor(ContextCompat.getColor(this, R.color.orange_f47b41));
        return view;
    }

    /***
     * 获取一个蓝色view显示在marker上面
     */
    public View getBlueView(String name) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_marker_base, null);
        RelativeLayout rl = view.findViewById(R.id.rl_main);
        rl.setBackgroundResource(R.mipmap.team_btn_location_blue_one);
        TextView mTvName = view.findViewById(R.id.tv_name);
        mTvName.setText(name);
        mTvName.setTextColor(ContextCompat.getColor(this, R.color.text_blue));
        return view;
    }

    /**
     * 解析定位的location参数
     */
    public void setLcoations(Location location) {

//        PeoPlesafefLocation loc = new PeoPlesafefLocation();
    }


    public void searchSite(String str, Inputtips.InputtipsListener var1) {
        InputtipsQuery inputquery = new InputtipsQuery(str, "");
        Inputtips inputTips = new Inputtips(this, inputquery);
        inputTips.setInputtipsListener(var1);
        inputTips.requestInputtipsAsyn();
    }

    /**
     * 删除秒点外的所有marker
     */
    public void deleteAllMarker() {
        for (int i = 0; i < aMap.getMapScreenMarkers().size(); i++) {
            Marker marker = aMap.getMapScreenMarkers().get(i);
            if (marker.getObject() != null &&  !marker.getObject().toString().equals("集合位置")) {
                Lg.i(TAG, "deleteAllMarker: " + i);
                marker.remove();
            }
        }
    }

    public void deleteMarker(String tag){
        for (int i = 0; i < aMap.getMapScreenMarkers().size(); i++) {
            Marker marker = aMap.getMapScreenMarkers().get(i);
            if (marker.getObject() != null && marker.getObject().toString().equals(tag)){
                marker.remove();
            }
        }
    }

    public int getTance(double lat1, double lng1, double lat2, double lng2) {
        lat1 = lat1 * (Math.PI / 180);
        lat2 = lat2 * (Math.PI / 180);
        lng1 = lng1 * (Math.PI / 180);
        lng2 = lng2 * (Math.PI / 180);
        double ban = 6371.0;
        double d = Math.acos(Math.sin(lat1) * Math.sin(lat2)
                + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lng2 - lng1)) * ban * 1000;
        return (int) d;
    }

    public Marker getMarker(String id) {
        for (int i = 0; i < aMap.getMapScreenMarkers().size(); i++) {
            Marker marker = aMap.getMapScreenMarkers().get(i);
            if (marker.getObject() != null && marker.getObject().toString().equals(id)) {
                return marker;
            }
        }
        return null;
    }

    //marker 点击事件
    @Override
    public boolean onMarkerClick(Marker marker) {
        return false;
    }


    // 显示marker窗口
    @Override
    public View getInfoContents(Marker marker) {
        return null;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }
}
